//
//  EUExBlueTooth.m
//  EUExBlueTooth
//
//  Created by zhijian du on 15/2/7.
//  Copyright (c) 2015年 zhijian du. All rights reserved.
//

#import "EUExBlueTooth.h"
#import "EUtility.h"
#import "SVProgressHUD.h"
#import <GameKit/GameKit.h>

@interface EUExBlueTooth () <GKPeerPickerControllerDelegate,GKSessionDelegate>

@property (nonatomic, retain) GKPeerPickerController *gkPickerController;
@property (nonatomic, retain) GKSession *currentSession;

@end

@implementation EUExBlueTooth

-(id)initWithBrwView:(EBrowserView *)eInBrwView{
    self = [super initWithBrwView:eInBrwView];
    if (self) {
        
    }
    return self;
}

-(void)open:(NSMutableArray *)inArguments{
    NSLog(@"ddd");
    GKPeerPickerController *pickerController = [[GKPeerPickerController alloc]init];
    self.gkPickerController = pickerController ;
    [pickerController release];
    self.gkPickerController.delegate = self;
    self.gkPickerController.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
    
    [self.gkPickerController show];
}


-(void)send:(NSMutableArray *)inArguments{
    if(self.currentSession){
        NSString *card = @"66666";
        NSString *money = @"99999999";
        NSString *msg = [NSString stringWithFormat:@"卡号：%@,金额：%@",card,money];
        NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];
        [self.currentSession sendDataToAllPeers:data withDataMode:GKSendDataReliable error:nil];
    }
}


-(void)close:(NSMutableArray *)inArguments{
    [self.currentSession disconnectFromAllPeers];
    self.currentSession = nil;
    self.gkPickerController.delegate = nil;
}
-(void)clean{

}
-(void)dealloc{
    
    [super dealloc];
}
#pragma mark - GKPeerPickerControllerDelegate
- (GKSession *) peerPickerController:(GKPeerPickerController *)picker
            sessionForConnectionType:(GKPeerPickerConnectionType)type {
    GKSession *currentSession = [[GKSession alloc] initWithSessionID:@"sunnada_CoreBlueToothDemo" displayName:nil sessionMode:GKSessionModePeer];
    self.currentSession = currentSession;
    [currentSession release];
    self.currentSession.delegate = self;
    
    return self.currentSession;
}
/* Notifies delegate that the user cancelled the picker.
 * 如果用户取消了蓝牙选择器
 */
- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker{
//    [SVProgressHUD showErrorWithStatus:@"蓝牙连接取消"];
    picker.delegate = nil;
    self.currentSession = nil;
}
//连接后
- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session{
    self.currentSession = session;
    session.delegate = self;
    [session setDataReceiveHandler:self withContext:nil];
    
    picker.delegate = nil;
    [picker dismiss];
}
#pragma mark - GKSessionDelegate
//设备断开或连接时
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state{
    switch (state) {
        case GKPeerStateConnected:
            [SVProgressHUD showSuccessWithStatus:@"蓝牙已连接"];
            break;
        case GKPeerStateDisconnected:
            [SVProgressHUD showSuccessWithStatus:@"蓝牙已断开"];
            self.gkPickerController.delegate = nil;
            self.currentSession = nil;
            break;
        default:
            break;
    }
}
//方法功能：接受数据
-(void)receiveData:(NSData*)data fromPeer:(NSString*)peer inSession:(GKSession*)session context:(void*)context{
    NSString* msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//    [SVProgressHUD showSuccessWithStatus:@"数据接收成功"];
    
    
    [super jsSuccessWithName:@"uexBlueTooth.receiveCallBack" opId:0 dataType:0 strData: msg];
}
@end
